//
//  BCUserNameController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/28.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCUserNameController.h"
//
#import "UIButton+UIActivityIndicatorView.h"

//request
#import "BCUpdateUserNicknameRequest.h"//更新用户信息请求

@interface BCUserNameController () <QMUITextFieldDelegate>

@property (nonatomic, strong) QMUITextField *nameTF;

@end

@implementation BCUserNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.title = BCLocalizedKey(@"user_info_name");
    //
    [self setupSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.nameTF becomeFirstResponder];
    });
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangedNotification:) name:UITextFieldTextDidChangeNotification object:self.nameTF];
    
    //
    self.nameTF.text = self.currentName;
}
- (void)setupSubviews {
    [self.customNaviBar.rightButton setTitle:BCLocalizedKey(@"Save") forState:UIControlStateNormal];
    //
    self.nameTF = [QMUITextField new];
    self.nameTF.textInsets = UIEdgeInsetsMake(0, 20, 0, 20);
    self.nameTF.shouldCountingNonASCIICharacterAsTwo = true;
    self.nameTF.maximumTextLength = 30.0;
    [self.view addSubview:self.nameTF];
    [self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom).offset(20.0);
        make.left.mas_equalTo(self.view).offset(10.0);
        make.right.mas_equalTo(self.view).offset(-10.0);
        make.height.mas_equalTo(44.0);
    }];
    self.nameTF.inputAccessoryView = [UIView new];
    self.nameTF.tintColor = self.nameTF.textColor;
    self.nameTF.returnKeyType = UIReturnKeyDone;
    self.nameTF.delegate = self;
    self.nameTF.enablesReturnKeyAutomatically = true;
    //
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = [UIColor separatorColor];
    [self.view addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.nameTF);
        make.bottom.mas_equalTo(self.nameTF);
        make.height.mas_equalTo(1.0);
    }];
}

- (void)showSaveButton:(BOOL)show {
    if (show) {
        if (!self.customNaviBar.rightButton.alpha) {
            self.customNaviBar.rightButton.alpha = 1;
        }
    }else {
        if (self.customNaviBar.rightButton.alpha) {
            self.customNaviBar.rightButton.alpha = 0;
        }
    }
}

#pragma mark - textFieldTextDidChangedNotification:
- (void)textFieldTextDidChangedNotification:(NSNotification *)noti {
    NSString *name = [self realNickname];
    //
    BOOL showSave = (name.length&&(![name isEqualToString:self.currentName]));
    [self showSaveButton:showSave];
}

- (NSString *)realNickname {
    //去掉头尾的空白字符
    return self.nameTF.text.qmui_trim;
}

#pragma mark -
- (void)didClickedNaviRightButton {
    //
    [self requestUpdateNameAction];
}


#pragma mark - QMUITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (self.customNaviBar.rightButton.alpha) {
        [self didClickedNaviRightButton];
    }
    return false;
}


//更新用户昵称
- (void)requestUpdateNameAction {
    [self.customNaviBar.rightButton showIndicator];
    //
    BCUpdateUserNicknameRequest *request = [BCUpdateUserNicknameRequest new];
    //new data
    request.nickname = [self realNickname];
    //original data
    request.uuid = [GeneralParameterManager sharedGeneral].uuid;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.customNaviBar.rightButton hiddenIndicator];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCUpdateUserInfoResponse *resp = result;
        [[BCUserManager sharedUser] updateUserLoginData:resp.data];
        //
        [wself.navigationController popViewControllerAnimated:true];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}




- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
